package sg.edu.nus.comp.icreate.ivletalk.utils.xml;

import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

public abstract class AbstractXmlParser implements XmlParser {
	protected static final String TAG_RESULT = "Results";
	protected static final String TAG_MODULE = "Data_Module";
	protected static final String TAG_ID = "ID";

	protected InputStream input;
	protected XmlPullParser parser;

	protected AbstractXmlParser(InputStream input) throws XmlPullParserException {
		this.input = input;
		parser = Xml.newPullParser();
		parser.setInput(input, null);
	}

	protected AbstractXmlParser() {
		parser = Xml.newPullParser();
	}

	protected boolean goToTag(String tag, int type) throws Exception {
		int eventType;
		do {
			eventType = parser.getEventType();
			if (eventType == type && tag.equals(parser.getName())) {
				parser.next();
				return true;
			}
			parser.next();
		} while (parser.getEventType() != XmlPullParser.END_DOCUMENT);
		return false;
	}

	public void setInput(InputStream is) throws XmlPullParserException {
		parser.setInput(is, null);
	}

	public abstract Object getResult();

}
